#!/usr/bin/env tclsh
source streamtestlib.tcl

# Define records, protocol and startup (text goes to files)
# The asynPort "device" is connected to a network TCP socket
# Talk to the socket with send/receive/assure
# Send commands to the ioc shell with ioccmd

set records {
    record (ai, "DZ:test1")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test1 device")
    }
    record (ai, "DZ:test2")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test2 device")
    }
    record (ai, "DZ:test3")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test3 device")
    }
    record (ai, "DZ:test4")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test4 device")
    }
}

set protocol {
    Terminator = LF;
    @mismatch {out "mismatch";}
    test1 {in "%f"; out "|%f|%10f|%.2f|%- 10.5f|% f|%+f|%-5.1f|%#f|"; }
    test2 {in "%6f%(DESC)s"; out "|%g|%(DESC)s|"; }
    test3 {in "% 6f%(DESC)s"; out "|%g|%(DESC)s|"; }
    test4 {in "%#f"; out "%f"; }
}

set startup {
}

startioc

set inf [format %f inf]
if [catch {set nan [format %f nan]}] {set nan nan}

process DZ:test1
send "3.14159265359\n"
assure "|3.141593|  3.141593|3.14| 3.14159  | 3.141593|+3.141593|3.1  |3.141593|\n"
process DZ:test1
send "-3.14159265359\n"
assure "|-3.141593| -3.141593|-3.14|-3.14159  |-3.141593|-3.141593|-3.1 |-3.141593|\n"
process DZ:test1
send "0\n"
assure "|0.000000|  0.000000|0.00| 0.00000  | 0.000000|+0.000000|0.0  |0.000000|\n"
process DZ:test1
send "NAN\n"
assure "|$nan|       $nan|$nan| $nan      | $nan|+$nan|$nan  |$nan|\n"
process DZ:test1
send "-Inf\n"
assure "|-$inf|      -$inf|-$inf|-$inf      |-$inf|-$inf|-$inf |-$inf|\n"
process DZ:test1
send "1e6\n"
assure "|1000000.000000|1000000.000000|1000000.00| 1000000.00000| 1000000.000000|+1000000.000000|1000000.0|1000000.000000|\n"
process DZ:test1
send "   -  3.14159265359\n"
assure "mismatch\n"
process DZ:test1
send "bla\n"
assure "mismatch\n"
process DZ:test1
send "  bla\n"
assure "mismatch\n"
process DZ:test1
send "-bla\n"
assure "mismatch\n"

process DZ:test2
send "3.14159265359\n"
assure "|3.1415|9265359|\n"
process DZ:test2
send "     3.14159265359\n"
assure "|3.1415|9265359|\n"
process DZ:test2
send "  10blabla\n"
assure "|10|blabla|\n"
process DZ:test2
send "  1\n"
assure "|1||\n"

process DZ:test3
send "3.14159265359\n"
assure "|3.1415|9265359|\n"
process DZ:test3
send "     3.14159265359\n"
assure "|3|.14159265359|\n"
process DZ:test3
send "  10blabla\n"
assure "|10|blabla|\n"
process DZ:test3
send "  1\n"
assure "|1||\n"

process DZ:test4
send "3.14159265359\n"
assure "3.141593\n"
process DZ:test4
send "     -3.14159265359\n"
assure "-3.141593\n"
process DZ:test4
send "   -  3.14159265359\n"
assure "-3.141593\n"
process DZ:test4
send "   +  3.14159265359\n"
assure "3.141593\n"
process DZ:test4
send "   +-  3.14159265359\n"
assure "mismatch\n"

finish
